library(leaflet)
Registered S3 method overwritten by 'htmlwidgets':
method from
print.htmlwidget tools:rstudio
###filter the dataset
df =
data %>%
filter(data$year >= 2019 & ZIPCODE!="" & DBA != "") %>%
mutate(GRADE=replace(GRADE, GRADE == "", NA))
Error in `filter()`:
! Problem while computing `..1 = data$year >= 2019 & ZIPCODE != ""
& DBA != ""`.
Caused by error in `mask$eval_all_filter()`:
! object 'ZIPCODE' not found
Backtrace:
1. ... %>% mutate(GRADE = replace(GRADE, GRADE == "", NA))
4. dplyr:::filter.data.frame(., data$year >= 2019 & ZIPCODE != "" & DBA != "")
5. dplyr:::filter_rows(.data, ..., caller_env = caller_env())
6. dplyr:::filter_eval(dots, mask = mask, error_call = error_call)
8. mask$eval_all_filter(dots, env_filter)
spdf_file <- geojson_read("data/zip_code_040114.geojson", what = "sp")
Error: File does not exist. Create it, or fix the path.
##### None Interactive map (Population by region)
ggplot() +
geom_polygon(data=spdf_data %>%
left_join(stats_df, c("id"="ZIPCODE")),
aes(x=long,
y=lat,
group=group,
fill=POPULATION),
color="white",
size=.2) +
theme_void() +
coord_map() +
scale_fill_distiller(palette = "YlGnBu", direction = 1) +
labs(title="Population in New York City",
subtitle="Neighborhoods are filled by population",
fill="Population")
ℹ The package `mapproj` is required for `coord_map()`
✖ Would you like to install it?
1: Yes
2: No
1
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/maps_3.4.0.tgz'
Content type 'application/x-gzip' length 3105764 bytes (3.0 MB)
==================================================
downloaded 3.0 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/mapproj_1.2.8.tgz'
Content type 'application/x-gzip' length 83346 bytes (81 KB)
==================================================
downloaded 81 KB
The downloaded binary packages are in
/var/folders/8g/h01d0z5d7pd4tl0y6y3d_7j80000gn/T//RtmpQXKOME/downloaded_packages

leaflet()%>%
addProviderTiles("CartoDB")%>%
#### First Layer of PolyGons
addPolygons(
data = spdf_file_2022 ,
weight = 0.5,
color = "black",
stroke=TRUE ,
opacity = 1 ,
fillColor = ~nc_pal(Total),
label = ~paste0 ('Total Critical Violation : ' , Total),
group = '2022',
highlight = highlightOptions(weight = 3, color = "red", bringToFront = T)
) %>%
#### Second Layer of PolyGons
addPolygons(
data = spdf_file_2021 ,
weight = 0.5,
color = "black",
stroke=TRUE ,
opacity = 1 ,
fillColor = ~nc_pal(Total),
label =~paste0 ('Total Critical Violation : ' , Total),
group = '2021',
highlight = highlightOptions(weight = 3, color = "red", bringToFront = T)
) %>%
addLayersControl(overlayGroups = c("2022", "2021"))%>%
addLegend( pal=nc_pal, values= spdf_file_2022$Total, opacity=0.9, title = "Critical", position = "bottomleft" )
Warning: Some values were outside the color scale and will be treated as NAWarning: Some values were outside the color scale and will be treated as NA
leaflet()%>%
addProviderTiles("CartoDB")%>%
#### First Layer of PolyGons
addPolygons(
data = spdf_file_2022 ,
weight = 0.5,
color = "black",
stroke=TRUE ,
opacity = 1 ,
fillColor = ~nc_pal(Total),
label = ~paste0 ('Total Critical Violation : ' , Total),
group = '2022',
highlight = highlightOptions(weight = 3, color = "red", bringToFront = T)
) %>%
#### Second Layer of PolyGons
addPolygons(
data = spdf_file_2021 ,
weight = 0.5,
color = "black",
stroke=TRUE ,
opacity = 1 ,
fillColor = ~nc_pal(Total),
label =~paste0 ('Total Violation : ' , Total),
group = '2021',
highlight = highlightOptions(weight = 3, color = "red", bringToFront = T)
) %>%
addLayersControl(overlayGroups = c("2022", "2021"))%>%
#####Third layer
addPolygons(
data = spdf_file_2020 ,
weight = 0.5,
color = "black",
stroke=TRUE ,
opacity = 1 ,
fillColor = ~nc_pal(Total),
label =~paste0 ('Total Violation : ' , Total),
group = '2020',
highlight = highlightOptions(weight = 3, color = "red", bringToFront = T)
) %>%
####Fourth
addPolygons(
data = spdf_file_2019 ,
weight = 0.5,
color = "black",
stroke=TRUE ,
opacity = 1 ,
fillColor = ~nc_pal(Total),
label =~paste0 ('Total Violation : ' , Total),
group = '2019',
highlight = highlightOptions(weight = 3, color = "red", bringToFront = T)
) %>%
addLayersControl(overlayGroups = c("2022", "2021",'2020', '2019'))%>%
addLegend( pal=nc_pal, values= spdf_file_2022$Total, opacity=0.9, title = "Count of Total Violation", position = "bottomleft" )
Warning: Some values were outside the color scale and will be treated as NAWarning: Some values were outside the color scale and will be treated as NA